﻿// 返回当前参数
// $(jq).mvcPage('option')

// 重新加载
// $(jq).mvcPage('reload')

// 默认参数
jQuery.mvcOption = {
    // 默认加载的页面索引
    pageIndex: 0,
    // 默认的页面大小
    pageSize: 10,
    // 默认post请求的地址(pageIndex和pageSize自动被追加到url)
    url: '',
    // 请求参数：对象或方法或字符串
    queryParam: {},
    // 获取页面html后回调方法
    callBack: undefined,
    // 显示字符串格式
    pageString: '第{current_page}页，共{max_page}页',
    // 分页控件html
    pageHtml: '<div class=\"mvcpage\"><a href=\"#\" class=\"first disabled\" title=\"首页\" data-action=\"first\">«</a><a href=\"#\" class=\"previous disabled\" title=\"上一页\" data-action=\"previous\">‹</a><input type=\"text\" data-max-page=\"0\"><a href=\"#\" class=\"next\" title=\"下一页\" data-action=\"next\">›</a><a href=\"#\" class=\"last\" title=\"尾页\" data-action=\"last\">»</a></div>'
};

(function (e) { var t = {}; e.fn.mvcPage = function (n) { function i() { if (e.isPlainObject(n)) return t = e.extend({}, e.mvcOption, n), s(); var i = (n || "").toLocaleString(); return i == "reload" ? (r.jqPagination("destroy"), s()) : i == "option" ? t : !1 } function s() { return r.html(t.pageHtml), o(t.pageIndex, function (e) { r.jqPagination({ current_page: t.pageIndex + 1, page_string: t.pageString, max_page: e, paged: function (e) { o(e - 1) } }) }), r } function o(n, r) { var i = "pageIndex=" + n + "&pageSize=" + t.pageSize, s = t.url.indexOf("?") < 0 ? "?" : "&", o = t.url + s + i, u = e.isFunction(t.queryParam) ? t.queryParam() : t.queryParam; e.post(o, u, function (n) { var i = Math.ceil(n.totalCount / t.pageSize); e.isFunction(r) == 1 && r(i), e.isFunction(t.callBack) == 1 && t.callBack(n.html) }) } var r = e(this); return i() } })(jQuery), function (e) { "use strict"; e.jqPagination = function (t, n) { var r = this; r.$el = e(t), r.el = t, r.$input = r.$el.find("input"), r.$el.data("jqPagination", r), r.init = function () { r.options = e.extend({}, e.jqPagination.defaultOptions, n), r.options.max_page === null && (r.$input.data("max-page") !== undefined ? r.options.max_page = r.$input.data("max-page") : r.options.max_page = 1), r.$input.data("current-page") !== undefined && r.isNumber(r.$input.data("current-page")) && (r.options.current_page = r.$input.data("current-page")), r.$input.removeAttr("readonly"), r.updateInput(!0), r.$input.on("focus.jqPagination mouseup.jqPagination", function (t) { if (t.type === "focus") { var n = parseInt(r.options.current_page, 10); e(this).val(n).select() } if (t.type === "mouseup") return !1 }), r.$input.on("blur.jqPagination keydown.jqPagination", function (t) { var n = e(this), i = parseInt(r.options.current_page, 10); t.keyCode === 27 && (n.val(i), n.blur()), t.keyCode === 13 && n.blur(), t.type === "blur" && r.setPage(n.val()) }), r.$el.on("click.jqPagination", "a", function (t) { var n = e(this); if (n.hasClass("disabled")) return !1; !t.metaKey && !t.ctrlKey && (t.preventDefault(), r.setPage(n.data("action"))) }) }, r.setPage = function (e, t) { if (e === undefined) return r.options.current_page; var n = parseInt(r.options.current_page, 10), i = parseInt(r.options.max_page, 10); if (isNaN(parseInt(e, 10))) switch (e) { case "first": e = 1; break; case "prev": case "previous": e = n - 1; break; case "next": e = n + 1; break; case "last": e = i } e = parseInt(e, 10); if (isNaN(e) || e < 1 || e > i) return r.setInputValue(n), !1; r.options.current_page = e, r.$input.data("current-page", e), r.updateInput(t) }, r.setMaxPage = function (e, t) { if (e === undefined) return r.options.max_page; if (!r.isNumber(e)) return console.error("jqPagination: max_page is not a number"), !1; if (e < r.options.current_page) return console.error("jqPagination: max_page lower than current_page"), !1; r.options.max_page = e, r.$input.data("max-page", e), r.updateInput(t) }, r.updateInput = function (e) { var t = parseInt(r.options.current_page, 10); r.setInputValue(t), r.setLinks(t), e !== !0 && r.options.paged(t) }, r.setInputValue = function (e) { var t = r.options.page_string, n = r.options.max_page; t = t.replace("{current_page}", e).replace("{max_page}", n), r.$input.val(t) }, r.isNumber = function (e) { return !isNaN(parseFloat(e)) && isFinite(e) }, r.setLinks = function (e) { var t = r.options.link_string, n = parseInt(r.options.current_page, 10), i = parseInt(r.options.max_page, 10); if (t !== "") { var s = n - 1; s < 1 && (s = 1); var o = n + 1; o > i && (o = i), r.$el.find("a.first").attr("href", t.replace("{page_number}", "1")), r.$el.find("a.prev, a.previous").attr("href", t.replace("{page_number}", s)), r.$el.find("a.next").attr("href", t.replace("{page_number}", o)), r.$el.find("a.last").attr("href", t.replace("{page_number}", i)) } r.$el.find("a").removeClass("disabled"), n === i && r.$el.find(".next, .last").addClass("disabled"), n === 1 && r.$el.find(".previous, .first").addClass("disabled") }, r.callMethod = function (t, n, i) { switch (t.toLowerCase()) { case "option": var s = { trigger: !0 }, o = !1; e.isPlainObject(n) && !i ? e.extend(s, n) : s[n] = i; var u = s.trigger === !1; return s.max_page !== undefined && (o = r.setMaxPage(s.max_page, u)), s.current_page !== undefined && (o = r.setPage(s.current_page, u)), o === !1 && console.error("jqPagination: cannot get / set option " + n), o; case "destroy": r.$el.off(".jqPagination").find("*").off(".jqPagination"); break; default: return console.error('jqPagination: method "' + t + '" does not exist'), !1 } }, r.init() }, e.jqPagination.defaultOptions = { current_page: 1, link_string: "", max_page: null, page_string: "Page {current_page} of {max_page}", paged: function () { } }, e.fn.jqPagination = function () { var t = this, n = Array.prototype.slice.call(arguments), r = !1; if (typeof n[0] == "string") return e.each(t, function () { var t = e(this).data("jqPagination"); r = t.callMethod(n[0], n[1], n[2]) }), r; t.each(function () { new e.jqPagination(this, n[0]) }) } }(jQuery); if (!console) { var console = {}, func = function () { return !1 }; console.log = func, console.info = func, console.warn = func, console.error = func }